<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2017-09-07
 * Time: 11:47
 */
namespace app\common\model;
use think\Model;

class Sms extends Model{

    // 开启自动写入时间戳
    protected $autoWriteTimestamp = true;
    protected $insert = ['addtime'];
    protected $type = [
        'addtime'    => 'timestamp',
        'endtime'    => 'timestamp',
    ];
    // 定义时间戳字段名
    protected $createTime = 'addtime';
    protected $updateTime = 'endtime';
    //  推荐站内信通知
    public function add_tui($data){
        $webdb = webdb();
        //  处理一级推广
        switch ($webdb['spread_money_class']){
            case '0':
                $money_class = '元';
                break;
            case '1':
                $money_class = $webdb['moneyname'];
                break;
            case '2':
                $money_class = $webdb['moneyname'];
                break;
        }
        $sms['title'] = "恭喜推荐新会员成功";
        $sms['fuid'] = "0";
        $sms['status'] = "0";
        $erspread = $spread = '';
        if(!empty($data['euid'])){
            $esms = $sms;
            $esms['uid'] = $data['euid'];
            // 瑜伽专用判断
            $ergroup_id = \model('UserData')->where('uid',$esms['uid'])->value('group_id');
            if(!empty($webdb['user_spread_er']) || $webdb['user_spread_er'] != '0'){

                if($ergroup_id != '6'){ // 瑜伽专用判断
                    $erspread = "获得{$webdb['user_spread_er']}{$money_class}";
                }
            }
            //  检测网站是否开启二级推广
            if($webdb['spread_class'] == '3'){
                $esms['content'] = "恭喜：您的下级会员推荐新会员成功!{$erspread}";
                $this->setAttr('id',null)->isUpdate(false)->save($esms);
            }
        }
        $pugroup_id = \model('UserData')->where('uid',$data['puid'])->value('group_id'); // 瑜伽专用判断
        if(!empty($webdb['user_spread']) || $webdb['user_spread'] != '0'){
            if($pugroup_id != '6'){ // 瑜伽专用判断
                $spread = "获得{$webdb['user_spread']}{$money_class}";
            }
        }
        $sms['uid'] = $data['puid'];
        $sms['content'] = "恭喜：推荐新会员成功！{$spread}";
        $this->setAttr('id',null)->isUpdate(false)->save($sms);
        return true;
    }
    //  注册新会员通知
    public function addsms($data){
        $sms = array(
            'title' => "有新会员【{$data['username']}】加入！",
            'content' => "有新会员【{$data['username']}】注册成功！",
            'uid' => '1',
            'fuid' => '0',
            'status' => '0',
        );
        $tsms = array(
            'title' => "欢迎注册！",
            'content' => "您好: 感谢您的注册,请遵守国家的法律法规及网站规定！",
            'uid' => $data['uid'],
            'fuid' => '0',
            'status' => '0',
        );
        $alladd[] = $sms;
        $alladd[] = $tsms;
        $this->saveAll($alladd);
        return true;
    }

}